package com.ruoyi.app.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.app.domain.AppHtPoints;
import com.ruoyi.app.domain.AppUser;
import com.ruoyi.app.domain.HtPhotoRecord;
import com.ruoyi.app.mapper.AppHtPointsMapper;
import com.ruoyi.app.mapper.AppUserMapper;
import com.ruoyi.app.mapper.HtPhotoRecordMapper;
import com.ruoyi.app.service.IHtPhotoRecordService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 * 拍照记录表Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-15
 */
@Service
public class HtPhotoRecordServiceImpl extends ServiceImpl<HtPhotoRecordMapper, HtPhotoRecord> implements IHtPhotoRecordService {
    @Autowired
    private HtPhotoRecordMapper htPhotoRecordMapper;
    @Autowired
    private AppUserMapper appUserMapper;
    @Autowired
    private AppHtPointsMapper appHtPointsMapper;

    /**
     * 查询拍照记录表
     *
     * @param id 拍照记录表主键
     * @return 拍照记录表
     */
    @Override
    public HtPhotoRecord selectHtPhotoRecordById(Integer id) {
        return htPhotoRecordMapper.selectHtPhotoRecordById(id);
    }

    /**
     * 查询拍照记录表列表
     *
     * @param htPhotoRecord 拍照记录表
     * @return 拍照记录表
     */
    @Override
    public List<HtPhotoRecord> selectHtPhotoRecordList(HtPhotoRecord htPhotoRecord) {
        return htPhotoRecordMapper.selectHtPhotoRecordList(htPhotoRecord);
    }

    /**
     * 新增拍照记录表
     *
     * @param htPhotoRecord 拍照记录表
     * @return 结果
     */
    @Override
    public int insertHtPhotoRecord(HtPhotoRecord htPhotoRecord) {
        //
        // Long userId = SecurityUtils.getUserId();
        // htPhotoRecord.setContent(JSON.toJSONString(htPhotoRecord.getList()));
        // htPhotoRecord.setUserId(Integer.valueOf(userId.toString()));
        // htPhotoRecord.setUserName(SecurityUtils.getUsername());
        htPhotoRecord.setCreateTime(DateUtils.getNowDate());
        return htPhotoRecordMapper.insertHtPhotoRecord(htPhotoRecord);
    }

    /**
     * 修改拍照记录表
     *
     * @param htPhotoRecord 拍照记录表
     * @return 结果
     */
    @Override
    public int updateHtPhotoRecord(HtPhotoRecord htPhotoRecord) {
        htPhotoRecord.setUpdateTime(DateUtils.getNowDate());
        return htPhotoRecordMapper.updateHtPhotoRecord(htPhotoRecord);
    }

    /**
     * 批量删除拍照记录表
     *
     * @param ids 需要删除的拍照记录表主键
     * @return 结果
     */
    @Override
    public int deleteHtPhotoRecordByIds(Integer[] ids) {
        return htPhotoRecordMapper.deleteHtPhotoRecordByIds(ids);
    }

    /**
     * 删除拍照记录表信息
     *
     * @param id 拍照记录表主键
     * @return 结果
     */
    @Override
    public int deleteHtPhotoRecordById(Integer id) {
        return htPhotoRecordMapper.deleteHtPhotoRecordById(id);
    }

    @Override
    @Transactional
    public AjaxResult insertApppHtPhotoRecord(HtPhotoRecord htPhotoRecord) {

        htPhotoRecord.setContent(JSON.toJSONString(htPhotoRecord.getList()));

        htPhotoRecord.setCreateTime(DateUtils.getNowDate());
        int i = htPhotoRecordMapper.insertHtPhotoRecord(htPhotoRecord);
        if (i != 0) {
            AppUser appUser = appUserMapper.selectAppUserById(Long.valueOf(htPhotoRecord.getUserId()));
            if (appUser == null) {
                return AjaxResult.error("用户信息为空");
            }

            if (appUser.getPoints() < 1) {
                return AjaxResult.error("积分不足");
            }
            int num = appUser.getPoints() - 1;

            AppHtPoints appHtPoints = new AppHtPoints();
            appHtPoints.setRemainingPoints(num);
            appHtPoints.setType(0);
            appHtPoints.setPointsTitle("导出文章");
            appHtPoints.setNum(1);
            appHtPoints.setUserId(Integer.valueOf(appUser.getId().toString()));
            appHtPoints.setUserName(appUser.getNickName());
            appHtPoints.setCreateTime(new Date());
            // 添加用户记录
            appHtPointsMapper.insertAppHtPoints(appHtPoints);
            appUser.setPoints(num);
            // 修改用户的积分
            appUserMapper.updateAppUser(appUser);
            return AjaxResult.success();
        }else {
            return AjaxResult.error("新增失败");
        }
    }
}

